package org.liaohailong.helloworld;

import android.util.Log;

import androidx.annotation.NonNull;

import java.util.List;

/**
 * Author: liaohailong
 * Time: 2021/4/25 09:38
 * Describe:
 */
public class AUtil {
    private static final String TAG = "AUtil";
    private static ILog sLog = new AndroidLog();

    public static void setLog(@NonNull ILog log) {
        AUtil.sLog = log;
    }

    public static void printCostInMs(Runnable task, String prefix) {
        long startInMs = System.currentTimeMillis();
        task.run();
        long costInMs = System.currentTimeMillis() - startInMs;
        log(prefix + " cost " + costInMs + "ms");
    }

    public static <E> void print100(List<E> list) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        int count = 0;
        for (E e : list) {
            count++;
            if (count > 100) break;
            sb.append(e.toString());
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("]");
        log("list size = " + list.size());
        log("top100 = " + sb.toString());
    }

    public static void print100(int[] arr) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        int count = 0;
        for (int e : arr) {
            count++;
            if (count > 100) break;
            sb.append(e);
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("]");
        log("list size = " + arr.length);
        log("top100 = " + sb.toString());
    }

    public static void print100(Object[] arr) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        int count = 0;
        for (Object e : arr) {
            count++;
            if (count > 100) break;
            sb.append(e);
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("]");
        log("list size = " + arr.length);
        log("top100 = " + sb.toString());
    }

    public static void log(String msg) {
        sLog.log(TAG, msg);
    }

    public interface ILog {
        void log(String tag, String msg);
    }

    public static final class AndroidLog implements ILog {

        @Override
        public void log(String tag, String msg) {
            Log.i(tag, msg);
        }
    }
}
